package com.globalwave.common.db;

import junit.framework.Assert;

import org.junit.Test;

import com.globalwave.base.BaseSO;
import com.globalwave.common.ArrayPageList;
import com.globalwave.system.entity.Dict;
import com.globalwave.test.BaseTestCase;

public class SimpleJDBCDAOTest extends BaseTestCase {

	@SuppressWarnings("unchecked")
	@Test
	public void insertAndUpdateObject() throws Exception {
		Dict dict = new Dict() ;
		dict.setDomain_("#Test_Dict") ;
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		this.jdbcDao.insert(dict) ;
		this.jdbcDao.getConnection().commit() ;
		
		Dict so = new Dict() ;
		so.setDomain_(dict.getDomain_()) ;
		
		dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNotNull(dict) ;
		Assert.assertNotNull(dict.getId()) ;
		

		this.jdbcDao.getConnection().setAutoCommit(false) ;
		dict.setDomain_("#Test_NewDict") ;
		this.jdbcDao.update(dict) ;
		this.jdbcDao.getConnection().commit() ;


		so = new Dict() ;
		so.setDomain_("#Test_Dict") ;
		
		dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNull(dict) ;
		

		so.setDomain_("#Test_NewDict") ;
		
		dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNotNull(dict) ;

		// test get function
		so = new Dict() ;
		so.setId(dict.getId()) ;
		Dict dict2 = (Dict)this.jdbcDao.get(so) ;

		Assert.assertEquals(dict.getDomain_(), dict2.getDomain_()) ;		
		
		// test query function 
		DictSO so2 = new DictSO() ;
		so2.setDomain_(dict.getDomain_()) ;
		so2.addDesc("domain_") ;
		dict2 = ((ArrayPageList<Dict>)this.jdbcDao.query(so2, Dict.class)).get(0) ;

		Assert.assertEquals(dict.getDomain_(), dict2.getDomain_()) ;
		
		
		// test delete function
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		this.jdbcDao.delete(dict) ;
		this.jdbcDao.getConnection().commit() ;

		dict = (Dict)this.jdbcDao.find(so) ;

		Assert.assertNull(dict) ;
		

	}
	
	
	@Test
	public void insertAndUpdateMap() throws Exception {
		MapBean dictMap = new MapBean() ;
		dictMap.setTable("CFG_DICT") ;
		dictMap.setPrimartyKey("ID") ;
		dictMap.setSequence("DICT_ID_SEQ") ;
		dictMap.put("domain_", "#Test_Dict") ;
		
		this.jdbcDao.getConnection().setAutoCommit(false) ;
		this.jdbcDao.insert(dictMap) ;
		this.jdbcDao.getConnection().commit() ;
		
		Dict so = new Dict() ;
		so.setDomain_("#Test_Dict") ;
		
		Dict dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNotNull(dict) ;
		Assert.assertNotNull(dict.getId()) ;
		

		this.jdbcDao.getConnection().setAutoCommit(false) ;
		dictMap.put("domain_", "#Test_NewDict") ;
		this.jdbcDao.update(dictMap) ;
		this.jdbcDao.getConnection().commit() ;


		so = new Dict() ;
		so.setDomain_("#Test_Dict") ;
		
		dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNull(dict) ;
		

		so.setDomain_("#Test_NewDict") ;
		
		dict = (Dict)this.jdbcDao.find(so) ;
		
		Assert.assertNotNull(dict) ;	
		
		

		dictMap = new MapBean() ;
		dictMap.setTable("CFG_DICT") ;
		dictMap.setPrimartyKey("ID") ;
		dictMap.put("ID", dict.getId()) ;
		

		this.jdbcDao.getConnection().setAutoCommit(false) ;
		this.jdbcDao.delete(dictMap) ;
		this.jdbcDao.getConnection().commit() ;

		dict = (Dict)this.jdbcDao.find(so) ;

		Assert.assertNull(dict) ;
	}
	
	public class DictSO extends BaseSO {
		private String domain_ ;
		public String getDomain_() {
			return domain_;
		}
		public void setDomain_(String domain_) {
			this.domain_ = domain_;
		}
	}
}
